import axios from "axios";
import dayjs from "dayjs";

const requestData = ({ start, end }) =>
  new Promise(
    (resolve, reject) => {
      axios
        .post("api/seat_hours_get.php", { start, end })
        .then((response) => {
          let content = response.data.content.map((cell) => {
            let end = cell.endTime ? dayjs(cell.endTime) : dayjs();
            let minutes = end.diff(cell.startTime, "minute");
            let hours = end.diff(cell.startTime, "hour", true);
            return {
              number: cell.recordID,
              name: cell.userName,
              uid: cell.userId,
              rank: cell.controlLevel,
              qualify: cell.qualification,
              license: cell.controlLevelId < 3 ? "是" : "否",
              seat: cell.seatCode,
              sector: cell.seatCode,
              // sector: cell.sectorCodes,
              post: cell.seatPost,
              role: cell.seatRole,
              begin: cell.startTime,
              end: cell.endTime
                ? cell.endTime
                : end.format("YYYY-MM-DD HH:mm:ss"),
              minutes: minutes,
              hours: hours,
              hoursRaw: hours,
              wage: 0,
              salary: 0,
              ratio: 0,
            };
          });
          // console.log("HourSummary", "requestData", content);
          resolve(content);
        })
        .catch((error) => {
          console.error(
            "HourSummary",
            "requestData",
            `from ${start} to ${end}`,
            error
          );
          reject(error);
        });
    }
  );

export default requestData;
